//
//  SYRegisterViewController.m
//  songyao
//
//  Created by work on 16/10/19.
//  Copyright © 2016年 saikuo. All rights reserved.
//

#import "SYRegisterViewController.h"

@interface SYRegisterViewController () {
    UITextField *_phoneTextField;
    UITextField *_passwordTextField;
    UITextField *_verificationTextField;
    UIButton    *_verificationButton;
    UIButton    *_registerButton;
    NSInteger    _time;
    NSTimer     *_timer;
    UILabel     *_protocolLabel;
    UILabel     *_loginLabel;
    UILabel     *_forgetPasswordLabel;
    UIButton    *_agreeButton;
    
    NSMutableArray<UIView *> *_backgroundViewArray;

}

@end

@implementation SYRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"注册";
    _backgroundViewArray = [NSMutableArray array];
    [self createNavigationBackItem:[UIImage imageNamed:@"navigationButtonReturn"]];
    [self createUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self endTimer];
}

- (void)createUI {
    for (NSInteger i = 0; i < 3; i++) {
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(15, 15 + (110 / 2.0f * i), SCREEN_WIDTH - 30, 40)];
        backgroundView.backgroundColor = [UIColor grayBackgroundColor];
        [self.view addSubview:backgroundView];
        [_backgroundViewArray addObject:backgroundView];
    }
    
    [_backgroundViewArray enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        switch (idx) {
            case 0: {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
                UIImage *image = [UIImage imageNamed:@"login_user_icon"];
                [obj addSubview:imageView];
                imageView.image = image;
                [imageView makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(obj.mas_left).offset(25 / 2.0f);
                    make.centerY.equalTo(obj.mas_centerY);
                    make.size.mas_equalTo(image.size);
                }];
                
                _phoneTextField = [[UITextField alloc] init];
                _phoneTextField.font = [UIFont textFieldInputFont];
                _phoneTextField.textColor = [UIColor contentColor];
                _phoneTextField.placeholder = @"输入手机号";
                _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
                _phoneTextField.clearsOnBeginEditing = YES;
                _phoneTextField.clearButtonMode = UITextFieldViewModeAlways;
                [obj addSubview:_phoneTextField];
                [_phoneTextField makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(imageView.mas_right).offset(22 / 2.0f);
                    make.right.equalTo(obj.mas_right).offset(-25 / 2.0f);
                    make.top.bottom.mas_equalTo(obj);
                }];
            }
                break;
            case 1: {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
                [obj addSubview:imageView];
                UIImage *image = [UIImage imageNamed:@"login_lock_icon"];
                imageView.image = image;
                [imageView makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(obj.mas_left).offset(25 / 2.0f);
                    make.centerY.equalTo(obj.mas_centerY);
                    make.size.mas_equalTo(image.size);
                }];
                
                _passwordTextField = [[UITextField alloc] init];
                _passwordTextField.font = [UIFont textFieldInputFont];
                _passwordTextField.textColor = [UIColor contentColor];
                _passwordTextField.placeholder = @"输入密码";
                _passwordTextField.clearsOnBeginEditing = YES;
                _passwordTextField.clearButtonMode = UITextFieldViewModeAlways;
                [obj addSubview:_passwordTextField];
                [_passwordTextField makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(imageView.mas_right).offset(22 / 2.0f);
                    make.right.equalTo(obj.mas_right).offset(-25 / 2.0f);
                    make.top.bottom.mas_equalTo(obj);
                }];
            }
                break;
            case 2: {
                _verificationButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [_verificationButton setBackgroundColor:[UIColor orangeNavControllerTintColor]];
                _verificationButton.titleLabel.textColor = [UIColor whiteColor];
                _verificationButton.titleLabel.font = [UIFont titleFont];
                [_verificationButton addTarget:self action:@selector(getVerification:)];
                [obj addSubview:_verificationButton];
                [_verificationButton setTitle:@"获取验证码"];
                [_verificationButton makeConstraints:^(MASConstraintMaker *make) {
                    make.right.top.bottom.mas_equalTo(obj);
                    make.width.equalTo(@(100 * SCREEN_WIDTH / 320.0));
                }];
                
                _verificationTextField = [[UITextField alloc] init];
                _verificationTextField.font = [UIFont textFieldInputFont];
                _verificationTextField.textColor = [UIColor contentColor];
                _verificationTextField.placeholder = @"输入验证码";
                _verificationTextField.keyboardType = UIKeyboardTypeNumberPad;
                _verificationTextField.clearsOnBeginEditing = YES;
                _verificationTextField.clearButtonMode = UITextFieldViewModeAlways;
                [obj addSubview:_verificationTextField];
                [_verificationTextField makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(obj.mas_left).offset(25 / 2.0f);
                    make.right.equalTo(_verificationButton.mas_left).offset(-25 / 2.0f);
                    make.top.bottom.mas_equalTo(obj);
                }];
            }
                break;
        }
    }];
    
    UIView *lastView = [_backgroundViewArray lastObject];
    _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_registerButton setBackgroundColor:[UIColor orangeNavControllerTintColor]];
    _registerButton.titleLabel.textColor = [UIColor whiteColor];
    _registerButton.titleLabel.font = [UIFont navControllerTitleFont];
    [_registerButton addTarget:self action:@selector(registerButtonClick:)];
    [self.view addSubview:_registerButton];
    [_registerButton setTitle:@"注册"];
    [_registerButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).offset(40);
        make.left.equalTo(self.view.mas_left).offset(15);
        make.right.equalTo(self.view.mas_right).offset(-15);
        make.height.equalTo(@(40));
    }];
    
    _protocolLabel = [[UILabel alloc] init];
    _protocolLabel.userInteractionEnabled = YES;
    _protocolLabel.textColor = [UIColor contentColor];
    _protocolLabel.font = [UIFont textFieldInputFont];
    _protocolLabel.text = @"我已阅读并同意用户协议";
    [self.view addSubview:_protocolLabel];
    [_protocolLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_registerButton.mas_left);
        make.bottom.equalTo(_registerButton.mas_top).offset(-15 / 2.0f);
    }];
    UITapGestureRecognizer *protocolTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(protocolTap:)];
    [_protocolLabel addGestureRecognizer:protocolTap];
    
    _agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"login_agree_icon"];
    UIImage *selectImage = [UIImage imageNamed:@"login_unagree_icon"];
    [_agreeButton setImage:image forState:UIControlStateNormal];
    [_agreeButton setImage:selectImage forState:UIControlStateSelected];
    [self.view addSubview:_agreeButton];
    [_agreeButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_protocolLabel.mas_right).offset(3);
        make.centerY.equalTo(_protocolLabel.mas_centerY);
        make.size.mas_equalTo(image.size);
    }];
    [_agreeButton addTarget:self action:@selector(agreeButtonClick:)];
    
    
    
    _loginLabel = [[UILabel alloc] init];
    _loginLabel.textColor = [UIColor contentColor];
    _loginLabel.font = [UIFont textFieldInputFont];
    _loginLabel.text = @"登录已有账号";
    [self.view addSubview:_loginLabel];
    [_loginLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_protocolLabel.mas_left);
        make.top.equalTo(_registerButton.mas_bottom).offset(10);
    }];
    _loginLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *loginTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginTap:)];
    [_loginLabel addGestureRecognizer:loginTap];
    
    _forgetPasswordLabel = [[UILabel alloc] init];
    _forgetPasswordLabel.textAlignment = NSTextAlignmentRight;
    _forgetPasswordLabel.textColor = [UIColor contentColor];
    _forgetPasswordLabel.font = [UIFont textFieldInputFont];
    _forgetPasswordLabel.text = @"忘记密码？";
    [self.view addSubview:_forgetPasswordLabel];
    [_forgetPasswordLabel makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_registerButton.mas_right);
        make.centerY.equalTo(_loginLabel.mas_centerY);
    }];
    _forgetPasswordLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *forgetTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(forgetTap:)];
    [_forgetPasswordLabel addGestureRecognizer:forgetTap];
}

- (void)agreeButtonClick:(id)sender {
    _agreeButton.selected = !_agreeButton.selected;

}

- (void)protocolTap:(id)sender {
    NSLog(@"用户协议");
}

- (void)loginTap:(id)sender {
    [self actionBack:sender];
}

- (void)forgetTap:(id)sender {
    NSLog(@"忘记密码");
}

- (void)getVerification:(UIButton *)button {
    _time = 5;
    _verificationButton.userInteractionEnabled = NO;
    _verificationButton.backgroundColor = [UIColor grayColor];
    [_verificationButton setTitle:[NSString stringWithFormat:@"%lds",(long)_time]];
    [self startTimer];
}

- (void)startTimer {
    
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeButtonStauts) userInfo:nil repeats:YES];
    }
}

- (void)endTimer {
    [_timer invalidate];
    _timer = nil;
    _verificationButton.userInteractionEnabled = YES;
    _verificationButton.backgroundColor = [UIColor orangeNavControllerTintColor];
    [_verificationButton setTitle:@"获取验证码"];
}

- (void)changeButtonStauts {
    _time--;
    if (_time == 0) {
        [self endTimer];
        return;
    }
    [_verificationButton setTitle:[NSString stringWithFormat:@"%lds",(long)_time]];
}

- (void)registerButtonClick:(UIButton *)button {
    
}

- (void)actionBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
